const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

// 定义页面配置
const pages = [
  {
    name: 'index',
    entry: './src/main.ts',
    template: './src/index.html',
  },
  {
    name: 'edit',
    entry: './src/edit.ts', // 假设你有一个 edit.ts 文件
    template: './src/edit.html', // 可选，如果没有模板，Webpack 会使用默认模板
  },
];

module.exports = {
  // 动态生成 entry
  entry: pages.reduce((config, page) => {
    config[page.name] = page.entry;
    return config;
  }, {}),
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  plugins: [
    // 动态生成 HtmlWebpackPlugin
    ...pages.map((page) => new HtmlWebpackPlugin({
      template: page.template,
      filename: `${page.name}.html`,
      chunks: [page.name], // 只引入当前页面的 chunk
    })),
    new CopyWebpackPlugin({
      patterns: [
        { from: './src/assets', to: 'assets' },
        {
          from: path.resolve(__dirname, 'node_modules/ispace_rscm/dist/index.js'),
          to: path.resolve(__dirname, 'dist/ispace_rscm/index.js'), // 输出到 dist/assets/
        },
      ],
    }),
  ],
  // 输出配置
  output: {
    filename: '[name].js', // 输出文件名（动态生成，如 main.js 和 edit.js）
    path: path.resolve(__dirname, 'dist'),
    clean: true,
  },
  mode: 'development', // 或 'production' 根据需要
  watch: false,  
  devtool: 'eval-source-map', // 或 'cheap-module-source-map'
};